package com.grace.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.grace.beans.LogType;
import com.grace.common.RequestHolder;
import com.grace.dao.SysLogMapper;
import com.grace.dao.SysRoleUserMapper;
import com.grace.dao.SysUserMapper;
import com.grace.model.*;
import com.grace.util.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysRoleUserService {

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysLogMapper sysLogMapper;

    /**
     * 取出来对应的user
     */
    public List<SysUser> getListByRoleId(int roleId) {
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        //取出当前角色对应的选中的用户列表
        return sysUserMapper.getUserListByIdList(userIdList);
    }

    /**
     * 改变用户
     */
    public void changeRoleUsers(int roleId, List<Integer> userIdList) {
        //根据查出来的角色id列表查权限id列表
        List<Integer> originUserIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        //判断是否相同, 使用Set来进行比对
        if (originUserIdList.size() == userIdList.size()) {
            Set<Integer> originUserIdSet = Sets.newHashSet(originUserIdList);
            Set<Integer> aclIdSet = Sets.newHashSet(userIdList);
            //移除之后，如果什么都不剩则完全一致
            originUserIdList.removeAll(aclIdSet);
            if (CollectionUtils.isEmpty(originUserIdList)) {
                return;
            }
        }
        //如果不相同, 则做更新操作
        updateRoleUsers(roleId, userIdList);

        //记录日志
        saveRoleUserLog(roleId, originUserIdList, userIdList);
    }

    /**
     * 更新角色-用户
     *
     * @param roleId
     * @param userIdList
     */
    @Transactional
    public void updateRoleUsers(int roleId, List<Integer> userIdList) {
        //先移除旧的,然后再添加重新添加
        sysRoleUserMapper.deleteByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        //将新的放在roleUserList中
        List<SysRoleUser> roleUserList = Lists.newArrayList();
        for (Integer userId : userIdList) {
            SysRoleUser sysRoleUser = SysRoleUser.builder().roleId(roleId).userId(userId).build();
            sysRoleUser.setOperator(RequestHolder.getCurrentUser().getUsername());
            sysRoleUser.setOperatorTime(new Date());
            roleUserList.add(sysRoleUser);
        }
        //调用批量增加的功能增加到角色权限表中
        sysRoleUserMapper.batchInsert(roleUserList);
    }

    /**
     * 保存角色_用户的日志信息
     */
    public void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_USER);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonMapper.obj2String(before));
        sysLog.setNewValue(after == null ? "" : JsonMapper.obj2String(after));
        sysLog.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLog.setOperatorTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insertSelective(sysLog);
    }
}
